<template>
  <el-row>
    <el-col :span="11">
      <el-breadcrumb separator="/" style="margin: 0 0 20px 0">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>代办公司</el-breadcrumb-item>
      </el-breadcrumb>
      <el-input placeholder="请输入ID" size="small" style="width: 40%"  v-model.trim="sousuo"></el-input>
      <el-button type="primary" size="small" @click="sousuoid()">搜索</el-button>
    </el-col>
    <el-col :span="11">
      <h1>代办公司</h1>
    </el-col>
    <el-col :span="2">
      <el-button type="primary" size="small" @click="dialogFormAdd = true">添加代办公司</el-button>
    </el-col>
    <el-col :span="24" style="margin:  10px 0 3% 0">
      <el-table
        :data="gsData"
        style="width: 100%;">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          prop="number"
          label="代办公司ID">
        </el-table-column>
        <el-table-column
          prop="name"
          label="公司名称">
        </el-table-column>
        <el-table-column
          prop="level"
          label="星级">
        </el-table-column>
        <el-table-column
          prop="phone"
          label="联系电话">
        </el-table-column>
        <el-table-column
          prop="address"
          label="联系地址">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="140">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="lookayCompany(scope.$index, scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="upayCompany(scope.$index, scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="delayCompany(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
    <el-col :span="24" style="text-align: center">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5,10]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <el-dialog title="添加代办公司" :visible.sync="dialogFormAdd">
        <el-form :model="formAdd" style="width: 80%" :rules="rules1" ref="formAdd" >
          <el-form-item label="公司名称 :" label-width="20%"  prop="name">
            <el-input v-model="formAdd.name"></el-input>
          </el-form-item>
          <el-form-item label="星级 :" label-width="20%" prop="level">
            <el-input v-model="formAdd.level" placeholder="星级最高为5"></el-input>
          </el-form-item>
          <el-form-item label="联系电话 :" label-width="20%" prop="phone">
            <el-input v-model="formAdd.phone"></el-input>
          </el-form-item>
          <el-form-item label="联系地址 :" label-width="20%" prop="address">
            <el-input v-model="formAdd.address"></el-input>
          </el-form-item>
          <el-form-item label="服务宣言 :" label-width="20%" prop="declaration">
            <el-input v-model="formAdd.declaration"></el-input>
          </el-form-item>
          <el-form-item label="公司简称 :" label-width="20%" prop="synopsis">
            <el-input
              type="textarea"
              :rows="8"
              placeholder="请输入内容"
              v-model="formAdd.synopsis">
            </el-input>
          </el-form-item>
          <el-form-item label="上传头像 :" label-width="20%">
            <el-upload
              class="avatar-uploader"
              action="https://xcx.wzlzfc.com/bbdj/house/uploadimage"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="formAdd.headImg" :src="formAdd.headImg" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px;"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="上传照片 :" label-width="20%">
            <el-upload
              class="avatar-uploader"
              action="https://xcx.wzlzfc.com/bbdj/house/uploadimage"
              :show-file-list="false"
              :on-success="handleAvatarSuccess1"
              :before-upload="beforeAvatarUpload">
              <img v-if="formAdd.image" :src="formAdd.image" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px;"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="resetForm('formAdd')">取 消</el-button>
          <el-button type="primary" @click="addayCompany('formAdd')">确 定</el-button>
        </div>
      </el-dialog>
      <el-dialog title="修改代办公司" :visible.sync="dialogFormUp">
        <el-form :model="formUp" style="width: 80%" :rules="rules1" ref="formUp">
          <el-form-item label="公司名称 :" label-width="20%" prop="name">
            <el-input v-model="formUp.name"></el-input>
          </el-form-item>
          <el-form-item label="星级 :" label-width="20%" prop="level">
            <el-input v-model="formUp.level" placeholder="星级最高为5"></el-input>
          </el-form-item>
          <el-form-item label="联系电话 :" label-width="20%" prop="phone">
            <el-input v-model="formUp.phone"></el-input>
          </el-form-item>
          <el-form-item label="联系地址 :" label-width="20%" prop="address">
            <el-input v-model="formUp.address"></el-input>
          </el-form-item>
          <el-form-item label="服务宣言 :" label-width="20%" prop="declaration">
            <el-input v-model="formUp.declaration"></el-input>
          </el-form-item>
          <el-form-item label="公司简介 :" label-width="20%" prop="synopsis">
            <el-input
              type="textarea"
              :rows="8"
              placeholder="请输入内容"
              v-model="formUp.synopsis">
            </el-input>
          </el-form-item>
          <el-form-item label="上传头像 :" label-width="20%">
            <el-upload
              class="avatar-uploader"
              action="https://xcx.wzlzfc.com/bbdj/house/uploadimage"
              :show-file-list="false"
              :on-success="handleAvatarSuccess2"
              :before-upload="beforeAvatarUpload">
              <img v-if="formUp.headImg" :src="formUp.headImg" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px;"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="上传照片 :" label-width="20%">
            <el-upload
              class="avatar-uploader"
              action="https://xcx.wzlzfc.com/bbdj/house/uploadimage"
              :show-file-list="false"
              :on-success="handleAvatarSuccess3"
              :before-upload="beforeAvatarUpload">
              <img v-if="formUp.image" :src="formUp.image" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px;"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="resetForm1('formUp')">取 消</el-button>
          <el-button type="primary" @click="upayCompany1('formUp')">修 改</el-button>
        </div>
      </el-dialog>
      <el-dialog title="查看代办公司" :visible.sync="dialogFormLook">
        <el-form :model="formLook" style="width: 80%">
          <el-form-item label="公司名称 :" label-width="20%">
            <el-input v-model="formLook.name"></el-input>
          </el-form-item>
          <el-form-item label="星级 :" label-width="20%">
            <el-input v-model="formLook.level" placeholder="星级最高为5"></el-input>
          </el-form-item>
          <el-form-item label="联系电话 :" label-width="20%">
            <el-input v-model="formLook.phone"></el-input>
          </el-form-item>
          <el-form-item label="联系地址 :" label-width="20%">
            <el-input v-model="formLook.address"></el-input>
          </el-form-item>
          <el-form-item label="服务宣言 :" label-width="20%">
            <el-input v-model="formLook.declaration"></el-input>
          </el-form-item>
          <el-form-item label="公司简介 :" label-width="20%">
            <el-input
              type="textarea"
              :rows="8"
              placeholder="请输入内容"
              v-model="formLook.synopsis">
            </el-input>
          </el-form-item>
          <el-form-item label="上传头像 :" label-width="20%">
            <el-upload
              class="avatar-uploader"
              action="https://xcx.wzlzfc.com/bbdj/house/uploadimage"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="formLook.headImg" :src="formLook.headImg" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px;"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="上传照片 :" label-width="20%">
            <el-upload
              class="avatar-uploader"
              action="https://xcx.wzlzfc.com/bbdj/house/uploadimage"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="formLook.image" :src="formLook.image" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px;"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormLook = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormLook = false">确 定</el-button>
        </div>
      </el-dialog>
    </el-col>
  </el-row>
</template>

<script>
  import {addCompany, getCompany, getCompanyid, upCompany, delCompany,cyqueryid} from "@/api/compayApi";

  export default {
    data() {
      let star = (rule, value, callback) => {
        if (/^[1-9]\d*$/.test(value) === false) {
          callback(new Error('必须是正整数'));
        } else if (value > 5) {
          callback(new Error('不能大于5'));
        } else {
          callback();
        }
      };
      return {
        dialogFormAdd: false,
        dialogFormUp: false,
        dialogFormLook: false,
        currentPage4: 1,
        total: 10,
        size: 5,
        page: 1,
        imageUrl: '',
        imageUrl1: '',
        imageUrl2: '',
        sousuo:'',
        formLook: {
          level: "",
          declaration: "",
          synopsis: "",
          image: "",
          name: "",
          phone: "",
          headImg: "",
          address: "",
        },
        formAdd: {
          level: "",
          declaration: "",
          synopsis: "",
          image: "",
          name: "",
          phone: "",
          headImg: "",
          address: "",
        },
        formUp: {
          level: "",
          declaration: "",
          synopsis: "",
          image: "",
          name: "",
          phone: "",
          headImg: "",
          address: "",
        },
        gsData: [],
        acid: '',
        rules1: {
          name: [
            {required: true, message: '请输入公司名称', trigger: 'blur'},
          ],
          level: [
            {required: true, message: '请输入星级', trigger: 'blur'},
            {validator: star, trigger: 'blur'},
          ],
          phone: [
            {required: true, message: '请输入手机号码', trigger: 'blur'},
          ],
          address: [
            {required: true, message: '请输入联系地址', trigger: 'blur'},
          ],
          declaration: [
            {required: true, message: '请输入服务宣言', trigger: 'blur'},
          ],
          synopsis: [
            {required: true, message: '请输入公司简称', trigger: 'blur'},
          ],

        }
      }
    },
    methods: {
      getayCompany() {
        let data = {
          page: this.page,
          size: this.size
        };
        this.gsData=[];
        getCompany(data).then((res) => {
          if(this.sousuo!==''){
            let data={number:this.sousuo};
            cyqueryid(data).then((res)=>{
              if(res.data.data!=null){
                let a=res.data.data;
                this.gsData.push(a);
              }
            })
          }else{
            this.gsData = res.data.data.content;
          }
          this.total = res.data.data.totalElements;
        })
      },
      addayCompany(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let data = this.formAdd;
            addCompany(data).then((res) => {
              this.getayCompany();
              this.resetForm(formName);
              this.$message({
                message: '添加代办公司成功!',
                type: 'success'
              });
            })
          } else {
            this.$message({
              message: '请填写完整!',
              type: 'warning'
            });
            return false;
          }
        });


      },
      lookayCompany(index, row) {
        this.formLook=[];
        let data = {agencyid: row.id};
        getCompanyid(data).then((res) => {
          this.formLook = res.data.data;
          this.dialogFormLook = true;
        })
      },
      upayCompany(index, row) {
        this.acid = row.id;
        let id = {agencyid: row.id};
        getCompanyid(id).then((res) => {
          this.formUp = res.data.data;
          this.dialogFormUp = true;
        })
      },
      upayCompany1(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let data = this.formUp;
            let id = {agencyid: this.acid};
            upCompany(id, data).then((res) => {
              this.getayCompany();
              this.resetForm1(formName);
              this.$message({
                message: '修改代办公司成功!',
                type: 'success'
              });
            })
          } else {
            this.$message({
              message: '请填写完整!',
              type: 'warning'
            });
            return false;
          }
        });
      },

      delayCompany(index, row) {
        let data = {agencyid: row.id};
        this.$confirm('确认删除吗?', '提示', {
          type: 'warning'
        }).then(() => {
          delCompany(data).then((res) => {
            this.getayCompany();
            this.$message({
              message: '删除代办公司成功!',
              type: 'success'
            });
          })
        }).catch(() => {
        });
      },
      sousuoid(){
        this.getayCompany();
      },

      handleSizeChange(val) {
        this.size = val;
        this.getayCompany();
      },
      handleCurrentChange(val) {
        this.page = val;
        this.getayCompany();
      },
      handleAvatarSuccess(res, file) {
        this.formAdd.headImg = res.data;
      },
      handleAvatarSuccess1(res, file) {

        this.formAdd.image = res.data;
      },
      handleAvatarSuccess2(res, file) {

        this.formUp.headImg = res.data;
      },
      handleAvatarSuccess3(res, file) {

        this.formUp.image = res.data;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      resetForm(formName) {
        this.dialogFormAdd = false;
        this.formAdd.image='';
        this.formAdd.headImg='';
        this.$refs[formName].resetFields();
      },
      resetForm1(formName) {
        this.dialogFormUp = false;
        this.formUp.image='';
        this.formUp.headImg='';
        this.$refs[formName].resetFields();
      },

    },
    mounted() {
      this.getayCompany();
    },
  }
</script>

<style>

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;

    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>

